/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Bitacora;

import SubsistemaPaciente.*;
import com.itextpdf.text.Document;
import com.itextpdf.text.Font;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import java.awt.Color;
import java.awt.Desktop;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;

/**
 *
 * @author Rodrigo
 */
public class DialogoInfoBitacora extends javax.swing.JDialog {

    /**
     * Creates new form DialogoInfoPaciente
     */
    private Bitacora bitacora;

    /**
     * constructor de la clase
     * @param parent al padre al cual va a responder
     * @param modal modo
     * @param bitacora corresponde al mantenedor de esta clase
     */
    public DialogoInfoBitacora(java.awt.Frame parent, boolean modal, Bitacora bitacora) {
        super(parent, modal);
        initComponents();
        setIconImage(new ImageIcon(getClass().getResource("/GUI/Img/logo2.png")).getImage());
        this.bitacora = bitacora;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        botonAceptar = new javax.swing.JButton();
        botonCancelar = new javax.swing.JButton();
        mensajeriaInformacion = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        nombre = new javax.swing.JTextField();
        modulo = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        rut = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        hora = new javax.swing.JTextField();
        fecha = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        accion = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        valorAnterior = new javax.swing.JTextArea();
        jScrollPane2 = new javax.swing.JScrollPane();
        valorNuevo = new javax.swing.JTextArea();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        botonAceptar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/GUI/Img/Iconos16X16/Aceptar.png"))); // NOI18N
        botonAceptar.setText("Aceptar");
        botonAceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonAceptarActionPerformed(evt);
            }
        });

        botonCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/GUI/Img/Iconos16X16/Cancelar.png"))); // NOI18N
        botonCancelar.setText("Cancelar");
        botonCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonCancelarActionPerformed(evt);
            }
        });

        mensajeriaInformacion.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        mensajeriaInformacion.setText("Área de mensajería");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Datos"));

        nombre.setEnabled(false);

        modulo.setEnabled(false);

        jLabel6.setText("Hora:");

        rut.setEnabled(false);

        jLabel4.setText("Acción:");

        jLabel3.setText("Módulo");

        jLabel5.setText("Fecha:");

        hora.setEnabled(false);

        fecha.setEnabled(false);

        jLabel2.setText("Nombre:");

        accion.setEnabled(false);

        jLabel1.setText("Rut:");

        jLabel7.setText("Valor anterior:");

        jLabel8.setText("Valor nuevo:");

        valorAnterior.setColumns(20);
        valorAnterior.setEditable(false);
        valorAnterior.setRows(5);
        jScrollPane1.setViewportView(valorAnterior);

        valorNuevo.setColumns(20);
        valorNuevo.setEditable(false);
        valorNuevo.setRows(5);
        jScrollPane2.setViewportView(valorNuevo);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel4)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3)
                            .addComponent(jLabel7))
                        .addGap(15, 15, 15)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 488, Short.MAX_VALUE)
                            .addComponent(hora, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(fecha, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(accion, javax.swing.GroupLayout.DEFAULT_SIZE, 488, Short.MAX_VALUE)
                            .addComponent(nombre, javax.swing.GroupLayout.PREFERRED_SIZE, 182, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(rut, javax.swing.GroupLayout.PREFERRED_SIZE, 118, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(modulo, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(jLabel6)
                    .addComponent(jLabel5)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel8)
                        .addGap(22, 22, 22)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 489, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(rut, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(nombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(modulo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel4)
                    .addComponent(accion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(20, 20, 20)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel7, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(22, 22, 22)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel5)
                    .addComponent(fecha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel6)
                    .addComponent(hora, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(19, 19, 19))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap(423, Short.MAX_VALUE)
                        .addComponent(botonAceptar, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(1, 1, 1)
                        .addComponent(botonCancelar))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(20, 20, 20)
                        .addComponent(mensajeriaInformacion, javax.swing.GroupLayout.DEFAULT_SIZE, 594, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 458, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(botonAceptar)
                    .addComponent(botonCancelar))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(mensajeriaInformacion)
                .addGap(19, 19, 19))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * @method que se encarga de oir el evento de aceptar de este jdialog
     * @param evt
     */
    private void botonAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonAceptarActionPerformed
        normalizarMensajeria();
        service.Bitacora b = new service.Bitacora();
        if (botonAceptar.getText().equalsIgnoreCase("Exportar")) {
            try {
                this.exportarPDF(b);
            } catch (Exception ex) {
                Logger.getLogger(Paciente.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        this.dispose();
    }//GEN-LAST:event_botonAceptarActionPerformed
    /*
     * @method evento que se encarga de los eventos del boton cancelar de este evento
     */
    private void botonCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonCancelarActionPerformed
        this.dispose();
    }//GEN-LAST:event_botonCancelarActionPerformed
    /*
     * @method metodo que se encarga de convertir un String a Date
     */

    public Date stringToDate(String strFecha) {
        SimpleDateFormat formatoDelTexto = new SimpleDateFormat("dd-MM-yyyy");
        Date fecha = null;
        try {
            fecha = formatoDelTexto.parse(strFecha);
        } catch (ParseException ex) {
            ex.printStackTrace();
        }
        return fecha;
    }

    /**
     * @method metodo que se encarga de exportar a pdf esta clase
     * @param b
     * @throws Exception
     */
    public void exportarPDF(service.Bitacora b) throws Exception {

        Font fuenteTitulo = new Font(Font.FontFamily.TIMES_ROMAN, 16, Font.BOLD);
        Font fuenteSubTitulo = new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.BOLD);
        Font fuenteTexto = new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.NORMAL);
        JFileChooser jF1 = new javax.swing.JFileChooser();
        String ruta;
        try {
            if (jF1.showSaveDialog(null) == jF1.APPROVE_OPTION) {
                ruta = jF1.getSelectedFile().getAbsolutePath() + ".pdf";
                PdfPTable tabla = new PdfPTable(2);
                tabla.addCell(new PdfPCell(new Paragraph("Rut Usuario", fuenteTexto)));
                tabla.addCell(new PdfPCell(new Paragraph(rut.getText(), fuenteTexto)));
                tabla.addCell(new PdfPCell(new Paragraph("Nombre", fuenteTexto)));
                tabla.addCell(new PdfPCell(new Paragraph(nombre.getText(), fuenteTexto)));
                tabla.addCell(new PdfPCell(new Paragraph("Módulo", fuenteTexto)));
                tabla.addCell(new PdfPCell(new Paragraph(modulo.getText(), fuenteTexto)));
                tabla.addCell(new PdfPCell(new Paragraph("Acción", fuenteTexto)));
                tabla.addCell(new PdfPCell(new Paragraph(accion.getText(), fuenteTexto)));
                tabla.addCell(new PdfPCell(new Paragraph("Valor Anterior", fuenteTexto)));
                tabla.addCell(new PdfPCell(new Paragraph(valorAnterior.getText(), fuenteTexto)));
                tabla.addCell(new PdfPCell(new Paragraph("Valor Nuevo", fuenteTexto)));
                tabla.addCell(new PdfPCell(new Paragraph(valorNuevo.getText(), fuenteTexto)));
                tabla.addCell(new PdfPCell(new Paragraph("Fecha", fuenteTexto)));
                tabla.addCell(new PdfPCell(new Paragraph(fecha.getText(), fuenteTexto)));
                tabla.addCell(new PdfPCell(new Paragraph("Hora", fuenteTexto)));
                tabla.addCell(new PdfPCell(new Paragraph(hora.getText(), fuenteTexto)));
                Document document = new Document();
                FileOutputStream ficheroPdf = new FileOutputStream(ruta);
                PdfWriter.getInstance(document, ficheroPdf).setInitialLeading(20);
                document.open();
                document.add(new Paragraph("Información Registro de Bitácora", fuenteTitulo));
                document.add(new Paragraph(" ", fuenteTexto));
                document.add(tabla);
                document.close();
                File f = new File(ruta);
                Desktop.getDesktop().open(f);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
/**
 * @method que obtiene el boton aceptar
 * @return
 */
    public JButton getBotonAceptar() {
        return this.botonAceptar;
    }
/**
 * @method que obtiene el boton cancelar
 * @return
 */
    public JButton getBotonCancelar() {
        return this.botonCancelar;
    }
/**
 * @method que cambia la mensajeria
 * @param s mensaje al cual se va a cambiar
 */
    public void cambiarMensajeria(String s) {
        this.mensajeriaInformacion.setText(s);
        mensajeriaInformacion.setForeground(Color.red);
    }
/**
 * @method que normaliza el area de mensajeria
 */
    public void normalizarMensajeria() {
        mensajeriaInformacion.setText("Área de mensajería");
        mensajeriaInformacion.setForeground(Color.BLACK);
    }
/**
 * metodo que llena los campos de este Jdialog
 * @param b que corresponde a una entidad bitacora
 */
    public void llenarCampos(service.Bitacora b) {
        this.limpiarCampos();
        rut.setText(b.getRut());
        this.nombre.setText(b.getNombre());
        this.modulo.setText(b.getModulo());
        this.accion.setText(b.getAccion());
        this.valorAnterior.setText(b.getValorAnterior());
        this.valorNuevo.setText(b.getValorNuevo());
        this.fecha.setText(b.getFecha());
        this.hora.setText(b.getHora());
    }
/**
 * @method que se encarga de limpiar los campos del jdialog
 */
    public void limpiarCampos() {
        rut.setText("");
        this.nombre.setText("");
        this.modulo.setText("");
        this.accion.setText("");
        this.valorAnterior.setText("");
        this.valorNuevo.setText("");
        this.fecha.setText("");
        this.hora.setText("");
    }
/**
 * metodo que inhabilita los campos
 */
    public void inhabilitarCampos() {
        rut.setEditable(false);
        this.nombre.setEditable(false);
        this.modulo.setEditable(false);
        this.accion.setEditable(false);
        this.valorAnterior.setEditable(false);
        this.valorNuevo.setEditable(false);
        this.fecha.setEditable(false);
        this.hora.setEditable(false);
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField accion;
    private javax.swing.JButton botonAceptar;
    private javax.swing.JButton botonCancelar;
    private javax.swing.JTextField fecha;
    private javax.swing.JTextField hora;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JLabel mensajeriaInformacion;
    private javax.swing.JTextField modulo;
    private javax.swing.JTextField nombre;
    private javax.swing.JTextField rut;
    private javax.swing.JTextArea valorAnterior;
    private javax.swing.JTextArea valorNuevo;
    // End of variables declaration//GEN-END:variables
}
